//作者类型「作者姓名「病区「序号 
function prepareAuthData(){
	var arr = ['F','S','T']
	var fDom = mui('#F')[0]
	var sStr = mui('#S')[0]
	var tStr = mui('#T')[0].getAttribute('data-id')
	var str = ''
	var index = 0
	for (var i = 0; i < 3;i++) {
		var dom = mui('#'+arr[i])[0]
		var childArr = dom.querySelectorAll('.oneAuth')
		for (var j = 0; j < childArr.length;j++) {
			if (j % 2 == 1) {
				str += childArr[j].getAttribute('data-id')+'「'+index
				index += 1
			}else{
				if(index > 0){
					str += ','
				}
				str += arr[i]+'「'+childArr[j].innerText+'「'
			}
		}
	}
	return str
}
//必填项保存和提交时没有填写的动画显示
function showEmptyAnimation(domId){
	mui('#'+domId)[0].classList.add('noContent');
	setTimeout(function() {
		mui('#'+domId)[0].classList.remove('noContent');
	}, 600);
}

function downListElementTapped(){
	blurAllInput()
	var thisDom = this
	if (thisDom.id.indexOf('Spec') != -1) {
		var arr = [{"SubCode":"是","SubDesc":"是","SubValue":"Y","SubColor":""},
			{"SubCode":"否","SubDesc":"否","SubValue":"N","SubColor":""}]
		updateOptionsUL(arr,thisDom)
		return
	}
	if (thisDom.id == 'TranLevel') {
		var arr = [{"SubCode":"院级","SubDesc":"院级","SubValue":"H","SubColor":""},
			{"SubCode":"科级","SubDesc":"科级","SubValue":"L","SubColor":""},
			{"SubCode":"病区","SubDesc":"病区","SubValue":"W","SubColor":""}]
		updateOptionsUL(arr,thisDom)
		return
	}
	var pdict = {'type':thisDom.getAttribute('data-type')}
	var paramDict = {'method':'web.INMDBComm:FindPubCode','params':JSON.stringify(pdict),'type':'Query'}
	//console.log(JSON.stringify(pdict))
	mui.DHCWebService(paramDict, function(result) {
		//console.log(JSON.stringify(result))
		var dataArr = result['data']
		updateOptionsUL(dataArr,thisDom)
	}, function(errorStr) {
	 	mui('#optionsPopo').popover('hide')
		mui.toast('请重试')
	});
}

function downWardListElementTapped(){
	blurAllInput()
	var thisDom = this
	var self = plus.webview.currentWebview()
	var perInfo = self.perInfo
	var pdict = {'nurseid':perInfo.loginID}
	var paramDict = {'method':'web.INMDataLimit:FindRoleWardList','params':JSON.stringify(pdict),'type':'RecQuery'}
	//console.log(JSON.stringify(pdict))
	mui.DHCWebService(paramDict, function(result) {
		//console.log(JSON.stringify(result))
		var wardArr = result['data']
		updateOptionsUL(wardArr,thisDom,'rw','WardDesc')
	}, function(errorStr) {
		mui.toast('请重试')
	});
}

function updateOptionsUL(dataArr,btnDom,idKey,textKey){
	var position = getAbsPosition(btnDom)
	var popoDom = mui('#optionsPopo')[0]
	popoDom.setAttribute('data-id',btnDom.id)
	var top = position['top'] + btnDom.clientHeight + 10 + mui('#wrapper').scroll().y
	var height = 40*dataArr.length;
	if (height < 80) {
		height = 80
	}else if (height > 200) {
		height = 200
	}
	popoDom.style.left = position['left']+'px'
	popoDom.style.top = top+'px'
	popoDom.style.width = btnDom.clientWidth + 'px'
	popoDom.style.height = height+'px'
	
	if (height + top > screen.height) {
		popoDom.style.top = (top - height - btnDom.clientHeight - 20)+'px'
	} 
	mui('#optionsPopo').popover('show')
	var html = ''
	for (var i= 0; i < dataArr.length;i++) {
		var oneDict = dataArr[i] //{"SubCode":"特等奖","SubDesc":"特等奖","SubValue":"38||1","SubColor":""}
		if (idKey) {
			html += '<li class="mui-table-view-cell" data-id="'+oneDict[idKey]+'">'+oneDict[textKey]+'</li>'
		}else{
			html += '<li class="mui-table-view-cell" data-id="'+oneDict['SubValue']+'">'+oneDict['SubDesc']+'</li>'
		}
		
	}
	mui('#optionsUL')[0].innerHTML = html;
	mui('#optionsUL li').each(function (index,element) {
		element.addEventListener('tap',optionElementTapped)
	});
}
//下拉表点击事件
function optionElementTapped(){
	var eleId = mui('#optionsPopo')[0].getAttribute('data-id');
	var eleDom = mui('#'+eleId)[0]
	eleDom.innerHTML = this.innerHTML
	eleDom.setAttribute('data-id',this.getAttribute('data-id'))
	if (eleId == 'OutWard') {
		$('#OutNurse')[0].innerHTML = ''
		$('#OutNurse')[0].setAttribute('data-id','')
	}
	mui('#optionsPopo').popover('hide')
}

//输入框失焦
function blurAllInput(){
	mui('body input').each(function (index,element) {
		if (element.type == 'file') {
			return
		}
		element.blur()
	})
	mui('body textarea').each(function (index,element) {
		element.blur()
	})
}
//选择日期
function selectDate(){
	blurAllInput()
	var _self = this
	if(_self.picker) {
		_self.picker.show(function(rs) {
			_self.innerText = rs.text
			_self.picker.dispose();
			_self.picker = null;
		});
	} else {
		var optionsJson = '{"type":"date"}';
		var options = JSON.parse(optionsJson);
		_self.picker = new mui.DtPicker(options);
		_self.picker.show(function(rs) {
			_self.innerText = rs.text
			_self.picker.dispose();
			_self.picker = null;
		});
	}
}
	
//主要用于键盘存在时,点击下拉列表重置一下弹框的位置
function windowResize(){
	if (!mui('#optionsPopo.mui-active')[0]) {
		return
	}
	setTimeout(function() {
		var popoDom = mui('#optionsPopo')[0]
		var btnDom = mui('#'+popoDom.getAttribute('data-id'))[0]
		var position = getAbsPosition(btnDom)
		var top = position['top'] + btnDom.clientHeight + 10 + mui('#wrapper').scroll().y
		var height = parseInt(popoDom.style.height)
		popoDom.style.top = top+'px'
		if (height + top > screen.height) {
			popoDom.style.top = (top - height - btnDom.clientHeight - 20)+'px'
		}
	}, 50);
}

function toChosePerson(domId,type,wardDomId){
	var pageId = plus.webview.currentWebview().id
	var extras = {'pageId':pageId,'domId':domId,'type':type}
	if (wardDomId) {
		extras['wardDesc'] = mui('#'+wardDomId)[0].innerText
		extras['wardId'] = mui('#'+wardDomId)[0].getAttribute('data-id')
		extras['wardDomId'] = wardDomId
	}
	var webview = plus.webview.create('../ky/kySelectPerson.html', 'kySelectPerson', {
		background: '#fff',
		statusbar: {
			background: '#fff'
		}
	},extras);
	webview.show('fade-in');
}

function setAuthorDomInfo(domId,params){
	// console.log(domId)
	// console.log(JSON.stringify(params))
	var dom = mui('#'+domId)[0]
	var id = ''
	var name=''
	for(var i = 0; i < params.length;i++){
		if(i > 0){
			id += ','
			name += ','
		}
		id +=params[i]['per']
		name += params[i]['perName']
	}
	dom.setAttribute('data-id',id)
	dom.innerHTML = name;
}